import json

import requests

import configReader
import dataParse
import stories


def loadLineInfo(key, searchId, page, host):
    url = 'https://' + host + '/s/horizon/flights/results/FlightSearchPollAction?p=0'

    headers = {
        'accept': '*/*',
        'accept-language': 'zh-CN,zh;q=0.9',
        'content-type': 'application/x-www-form-urlencoded',
        'cookie': configReader.getCookie(),
        'origin': 'https://' + host + '',
        'priority': 'u=1, i',
        'referer': 'https://www.cn.kayak.com/flights/TPE-TYO/2024-08-23/2024-08-30?sort=bestflight_a&attempt=1&lastms=1721826303563',
        'sec-ch-ua': '"Not/A)Brand";v="8", "Chromium";v="126", "Google Chrome";v="126"',
        'sec-ch-ua-mobile': '?0',
        'sec-ch-ua-platform': '"Windows"',
        'sec-fetch-dest': 'empty',
        'sec-fetch-mode': 'cors',
        'sec-fetch-site': 'same-origin',
        'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36',
        'x-csrf': 'JCBnR4n$zHqvClRaOvlQWujfeoeeAEu3pqOScnuOLa4-n3uwwrYRTl25KjPe2tDumBLAbPZ3ooM8KsWkoIIZOOk',
        'x-requested-with': 'XMLHttpRequest'
    }

    data = {
        'priceType': 'daybase',
        'searchId': searchId,
        'url': key,
        'pageNumber': page,
        'pollNumber': 0,
        'requestReason': 'POLL',
        'isSecondPhase': False,
        'sortMode': 'bestflight',
        'ascending': True
    }
    response = requests.post(url, data=data, headers=headers)
    json_response = response.json()

    FlightResultsList = json_response['FlightResultsList']
    return dataParse.parseFlightResults(key, FlightResultsList)
